<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 2017/1/6
 * Time: 下午6:33
 */

namespace EWeiLink\JuPai\Http\Controllers;

use EWeiLink\JuPai\JpControllerTrait;
use EWeiLink\JuPai\JuPai;
use EWeiLink\JuPai\Http\Requests\SupportRequest;
use EWeiLink\JuPai\OptionsEntity;
use EWeiLink\JuPai\Planner;
use EWeiLink\JuPai\Services\SupportService;
use EWeiLink\JuPai\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SupportController
{
    use JpControllerTrait;
    /**
     * @param  \Illuminate\Http\Request  $request
     * 认证并获取access_token
     */
    public function auth(SupportRequest $request)
    {
        
        $filter = JuPai::callBridgeHelper('get_filter', []);
        if (is_null($filter)) {
            $filter = [];
        }
        $plannerId = request('plannerId');
        $planner = Planner::where('plannerId', $plannerId)->first();
        if (is_null($planner)) {
            return $this->_error('无此理财师');
        }

        $clientId = request('clientId');
        $client_model = DB::table('oauth_clients')
            ->where('_id', $clientId)
            ->first();
        if (is_null($client_model)) {
            return $this->_error('无此clientId');
        }

        $secret = $client_model['secret'];
        $argumentSignatures = md5($clientId.$plannerId.$secret);
        // 获取请求头中的数据
        /**@var Request $request*/
        $request = request();
        $authorization = $request->bearerToken();
        if (empty($authorization)) {
            return $this->_error('认证失败，参数签名错误');
        }
        // 判断参数签名是否正确
        if (strcmp($authorization, $argumentSignatures) !== 0) {
            return $this->_error('认证失败，参数签名错误');
        }
        $user_id = $planner->user_id;
        $user = User::find($user_id);
        if (!is_null($user)) {
            $user = $user->getAppInstance();
        } else {
            return $this->_error("无对应的user_id");
        }

        if (is_null($user)) {
            return $this->_error('此理财师对应的user_id不正确');
        }

        $prefix = 'tk_';
        $supportService = new SupportService();
        $access_token = $supportService->createAccessToken($user, $prefix, $filter);

        $returnResult = [];
        if (!empty($access_token)) {
            $returnResult['access_token'] = $access_token;
        } else {
            return $this->_error('获取access_token失败');
        }

        return $returnResult;

    }

    /**
     * 根据理财师ID，返回基本信息
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function initOptions(SupportRequest $request)
    {
        $env = $request['env'];
        // 当前登录账户
        $user = request()->user();
        // 根据当前登录账户找到对应的理财师
        $planner = Planner::findByUser($user)->first();
        if (!$planner) {
            return $this->_error('无此理财师');
        }

        // 获取针对钜派SDK的配置信息
        $options = new OptionsEntity($env);
        return compact('planner', 'options');
    }

    public function contacts()
    {

    }
}